package com.cn.lottery.Server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cn.lottery.Gift;
import com.cn.lottery.GiftData;
import com.cn.lottery.LotteryUtil;
import com.cn.lottery.dao.LotteryDao;
import com.cn.lottery.domain.LotteryDO;
import com.cn.lottery.domain.LotteryPersionDO;
import com.cn.lottery.domain.PersionLotterVO;
import com.cn.lottery.domain.PrizeDO;
import com.cn.lottery.domain.TimeSettingDO;
import com.cn.lottery.domain.UsersDO;

@Service("server")
public class LotteryServer implements Server{
	
	@Resource
	private LotteryDao lotteryDao;
	
	public Map lotteryStart(String openid) {
		Map resoutMap = new HashMap();
		if(openid == null || openid.trim().equals("")){
			resoutMap.put("resout", false);
			resoutMap.put("resoutMsg", "用户无效");
			return resoutMap;
		}
		StringBuffer sb = new StringBuffer();
		Gift gift = null;
		int timeIndex = GiftData.getTimeIndex(this);
		if(timeIndex > 0){
			UsersDO user = new UsersDO();
			user.setOpenid(openid);
			user.setTimeIdx(timeIndex);
			List<UsersDO> userList = lotteryDao.searchUsers(user);
			if(userList != null && userList.size() > 0){
				resoutMap.put("resout", false);
				resoutMap.put("resoutMsg", "今日已抽奖");
			}else{
				//每人抽三次,总概率0.006
				for(int i=0 ; i < 3 ;i++){
					gift = LotteryUtil.lottery(Math.random(),this);
					if(gift == null){
						continue;
					}else{
						break;
					}
				}
				lotteryDao.insertUsers(user);
				if(gift != null){
					LotteryPersionDO persion = new LotteryPersionDO();
					persion.setLotteryId(gift.getId());
					persion.setLotteryIndex(gift.getSequence());
					persion.setOpenId(openid);
					lotteryDao.insertPersion(persion);
					
					PrizeDO prizedo = new PrizeDO();
					prizedo.setId(gift.getPrize().getId());
					prizedo.setResiduePool(gift.getResidue_pool());
					lotteryDao.updatePrize(prizedo);
					Map prize = new HashMap();
					prize.put("sequence", gift.getSequence());
					prize.put("name", gift.getName());
					prize.put("sequence", gift.getSequence());
					prize.put("index", (gift.getPool_size()-gift.getResidue_pool()));
					resoutMap.put("resout", true);
					resoutMap.put("prize", prize);
				}else{
					resoutMap.put("resout", false);
					resoutMap.put("resoutMsg", "很遗憾未抽中,欢迎明天再来");
				}
			}
		}else{
			resoutMap.put("resout", false);
			resoutMap.put("resoutMsg", "非抽奖时段");
		}
		return resoutMap;
	}

	public List<LotteryDO> searchGiftList(Integer timeIndex) {
		return lotteryDao.searchList(timeIndex);
	}

	@Override
	public List<TimeSettingDO> searchTimeSetting() {
		return lotteryDao.searchTimeSetting();
	}

	@Override
	public List<PersionLotterVO> searchLottery(String openid) {
		if(openid == null || openid.trim().equals("")){
			return null;
		}
		UsersDO user = new UsersDO();
		user.setOpenid(openid);
		return lotteryDao.searchLottery(user);
	}

	@Override
	public List<PrizeDO> searchPrize(Integer timeIndex) {
		return lotteryDao.searchPrize(timeIndex);
	}

	@Override
	public void createPrize(Integer timeIndex) {
		lotteryDao.createPrize(timeIndex);
	}
	
}
